local game = require "club.threecard.blackjack"
local GameObjectExtension = CS.Blaze.GameObjectExtension
local Vector3 = CS.UnityEngine.Vector3

local bet_chip = game.ui.element()

bet_chip.store_watchers = 
{
    ingame = 
    {
        ["阶段变更"] = function(self, store, state, args, initialized)
            if initialized then
                return;
            end
    
            if state.phase == state.PHASE.IDLE or state.phase == state.PHASE.SETTLE or state.phase == state.PHASE.NULL then
                self:clear();
            end
        end
    }
}

function bet_chip:ctor()
    self.res = "club.threecard.blackjack/ui/ingame.b:bet_chip"
    self.parents = {};
end

local function get_bet_temp(self, amount)
    local temp = nil;

    local bet_index = 1;
    for index, m in ipairs(game.store.ingame.state.bet_amounts) do
        if m == amount then
            bet_index =  index;
            break;
        end
    end
    
    if bet_index == 1 then
        temp = self.chip_1;
    elseif bet_index == 2 then
        temp = self.chip_2;
    elseif bet_index == 3 then
        temp = self.chip_3;
    elseif bet_index == 4 then
        temp = self.chip_4;
    elseif bet_index == 5 then
        temp = self.chip_5;
    else
        temp = self.chip_1;
    end

    return temp;
end

function bet_chip:payout(seat, amount, over_handler)
    local temp = get_bet_temp(self, amount);
    local obj = GameObjectExtension.GetFromPool(temp);
    obj.transform:SetParent(self.transform, false);
    obj.transform.localPosition = Vector3.zero;
    
    if amount > 0 then
        end_pos = seat.transform.position;    
    else
        end_pos = self.transform.position;
        obj.transform.position = seat.bet_root.position;
    end

    local end_pos = seat.transform.position;
    obj.transform:DOMove(end_pos, 0.8)
    :OnComplete(function()
        GameObjectExtension.ReturnToPool(obj);
        if over_handler then
            over_handler();
        end
    end)
end

function bet_chip:bet(parent, amount)
    if not self.loaded then
        return;
    end

    local temp = get_bet_temp(self, amount);

    local obj = GameObjectExtension.GetFromPool(temp);
    obj.transform:SetParent(parent, false);

    if not self.parents[parent] then
        self.parents[parent] = {};
    end

    table.insert(self.parents[parent], obj);

    local interval = 2;
    for i, o in ipairs(self.parents[parent]) do
        o.transform.localPosition = Vector3(0, interval*(i-1), 0);
    end

    game.audio.post("blackjack:put_down_chips");
    return self.parents[parent];
end

function bet_chip:insure(seat, amount)
    local temp = get_bet_temp(self, amount);
    local obj = GameObjectExtension.GetFromPool(temp);
    obj.transform:SetParent(self.transform, false);
    obj.transform.position = seat.transform.position;

    local end_pos  = self.transform.position;
    obj.transform:DOMove(end_pos, 0.8)
    :OnComplete(function()
        GameObjectExtension.ReturnToPool(obj);
    end)
end

function bet_chip:clear()
    for _, parent in pairs(self.parents) do
        for _, obj in pairs(parent) do
            GameObjectExtension.ReturnToPool(obj);
        end
    end

    self.parents = {};
end

return bet_chip